﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RestaurantTabletApplication
{
    public partial class RestaurentView : Form
    {
        public delegate void setTableNo(int no);

        private RestaurantWebService service;
        private IList<string> occupiedTables = new List<string>();
        private IList<TableInfo> tableInfos = new List<TableInfo>();
        private IDictionary<PictureBox, TableInfo> tableMap = new Dictionary<PictureBox, TableInfo>();
        
        public RestaurentView(RestaurantWebService service)
        {
            this.service = service;
            InitializeComponent();
            fillTableMap();
        }

        private void fillTableMap()
        {
            tableInfos.Add(new TableInfo("01", table01, RestaurantTabletApplication.Properties.Resources.t_g01, RestaurantTabletApplication.Properties.Resources.t_y01));
            tableInfos.Add(new TableInfo("02", table02, RestaurantTabletApplication.Properties.Resources.t_g02, RestaurantTabletApplication.Properties.Resources.t_y02));
            tableInfos.Add(new TableInfo("03", table03, RestaurantTabletApplication.Properties.Resources.t_g03, RestaurantTabletApplication.Properties.Resources.t_y03));
            tableInfos.Add(new TableInfo("04", table04, RestaurantTabletApplication.Properties.Resources.t_g04, RestaurantTabletApplication.Properties.Resources.t_y04));
            tableInfos.Add(new TableInfo("05", table05, RestaurantTabletApplication.Properties.Resources.t_g05, RestaurantTabletApplication.Properties.Resources.t_y05));
            tableInfos.Add(new TableInfo("06", table06, RestaurantTabletApplication.Properties.Resources.t_g06, RestaurantTabletApplication.Properties.Resources.t_y06));
            tableInfos.Add(new TableInfo("07", table07, RestaurantTabletApplication.Properties.Resources.t_g07, RestaurantTabletApplication.Properties.Resources.t_y07));
            tableInfos.Add(new TableInfo("08", table08, RestaurantTabletApplication.Properties.Resources.t_g08, RestaurantTabletApplication.Properties.Resources.t_y08));
            tableInfos.Add(new TableInfo("09", table09, RestaurantTabletApplication.Properties.Resources.t_g09, RestaurantTabletApplication.Properties.Resources.t_y09));
            tableInfos.Add(new TableInfo("10", table10, RestaurantTabletApplication.Properties.Resources.t_g10, RestaurantTabletApplication.Properties.Resources.t_y10));
            tableInfos.Add(new TableInfo("11", table11, RestaurantTabletApplication.Properties.Resources.t_g11, RestaurantTabletApplication.Properties.Resources.t_y11));
            tableInfos.Add(new TableInfo("12", table12, RestaurantTabletApplication.Properties.Resources.t_g12, RestaurantTabletApplication.Properties.Resources.t_y12));
            tableInfos.Add(new TableInfo("13", table13, RestaurantTabletApplication.Properties.Resources.t_g13, RestaurantTabletApplication.Properties.Resources.t_y13));
            tableInfos.Add(new TableInfo("14", table14, RestaurantTabletApplication.Properties.Resources.t_g14, RestaurantTabletApplication.Properties.Resources.t_y14));
            tableInfos.Add(new TableInfo("15", table15, RestaurantTabletApplication.Properties.Resources.t_g15, RestaurantTabletApplication.Properties.Resources.t_y15));

            foreach (TableInfo t in tableInfos)
            {
                tableMap.Add(t.PictureBox, t);
            }
        }

        private void onOccupy(object sender, EventArgs e)
        {
            PictureBox pictureBox = (PictureBox) sender;
            TableInfo tableInfo = tableMap[pictureBox];
            pictureBox.Image = tableInfo.OccupiedImage;
            occupiedTables.Add(tableInfo.TableNo);
            serving.Items.Add(tableInfo.TableNo.ToString());
        }

        private void onVacancy(object sender, EventArgs e)
        {
            PictureBox pictureBox = (PictureBox) sender;
            TableInfo tableInfo = tableMap[pictureBox];
            pictureBox.Image = tableInfo.VacantImage;
            occupiedTables.Remove(tableInfo.TableNo);
            serving.Items.Remove(tableInfo.TableNo.ToString());
        }

        private void servingStarted(object sender, EventArgs e)
        {
            TableView view = new TableView(serving.SelectedItem.ToString(), service);
            view.ShowDialog();          
        }
    }

    public class TableInfo
    {
        private string tableNo;
        private PictureBox pictureBox;
        private Bitmap occupiedImage;
        private Bitmap vacantImage;

        public TableInfo(string tableNo, PictureBox pictureBox, Bitmap occupiedImage, Bitmap vacantImage)
        {
            this.tableNo = tableNo;
            this.pictureBox = pictureBox;
            this.occupiedImage = occupiedImage;
            this.vacantImage = vacantImage;
        }

        public string TableNo
        {
            get { return tableNo; }
            set { tableNo = value; }
        }
        public PictureBox PictureBox
        {
            get { return pictureBox; }
            set { pictureBox = value; }
        }
        public Bitmap OccupiedImage
        {
            get { return occupiedImage; }
            set { occupiedImage = value; }
        }
        public Bitmap VacantImage
        {
            get { return vacantImage; }
            set { vacantImage = value; }
        }
    }
}
